package com.qike.duola.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qike.duola.domain.DuolaProject;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.DuolaCustomerService;
import com.qike.duola.service.DuolaDeptService;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "客户控制器")
@CrossOrigin
public class CustomerController {

    private static Logger logger = LoggerFactory.getLogger(CustomerController.class);

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private DuolaCustomerService customerService;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加客户
     *
     * @return
     */
    @PostMapping("/customerAdd")
    @ApiOperation("添加客户")
    @ResponseBody
    public BaseResult customerAdd(@RequestBody CustomerAddDto customerAddDto) {
        try {
            Integer uid = customerService.customerAdd(customerAddDto);
            return RetResponse.makeOKRsp(uid);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 编辑客户
     *
     * @return
     */
    @PostMapping("/customerEdit")
    @ApiOperation("编辑客户")
    @ResponseBody
    public BaseResult customerEdit(@RequestBody CustomerEditDto customerEditDto) {
        try {
            customerService.customerEdit(customerEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 客户列表
     *
     * @return
     */
    @GetMapping("/customerList")
    @ApiOperation("客户列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = CustomerListVo.class)
    })
    public BaseResult deptList(CustomerListDto customerListDto) {
        try {
            CustomerListVo vo = customerService.customerList(customerListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 客户名称下拉列表
     *
     * @return
     */
    @GetMapping("/customerPullList")
    @ApiOperation("客户名称下拉列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EnumList.class)
    })
    @Cacheable(value = {"customerPullList"})
    public BaseResult customerPullList(String name) {
        try {
            List<EnumList> names = customerService.customerPullList(name);
            return RetResponse.makeOKRsp(names);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 联系人下拉列表
     *
     * @return
     */
    @GetMapping("/contractPullList")
    @ApiOperation("联系人下拉列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EnumList.class)
    })
    @Cacheable(value = {"contractPullList"})
    public BaseResult contractPullList(String name) {
        try {
            List<EnumList> names = customerService.contractPullList(name);
            return RetResponse.makeOKRsp(names);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 客户详情
     *
     * @return
     */
    @GetMapping("/customerInfo")
    @ApiOperation("客户详情")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = CustomerInfoVo.class)
    })
    public BaseResult customerInfo(Integer id) {
        try {
            CustomerInfoVo vo = customerService.customerInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 跟进记录
     *
     * @return
     */
    @GetMapping("/followList")
    @ApiOperation("跟进记录")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = FollowListVo.class)
    })
    public BaseResult followList(Integer id) {
        try {
            FollowListVo vo = customerService.followList(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 添加跟进记录
     *
     * @return
     */
    @PostMapping("/AddFollowList")
    @ApiOperation("添加跟进记录")
    @ResponseBody
    public BaseResult addFollowList(@RequestBody AddFollowListDto addFollowListDto) {
        try {
            customerService.addFollowList(addFollowListDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
